

from puppy.model import Query, Response, Item


def search(query):
    """Echo search funcion.
  
    Parameters:

    * query (puppy.model.OpenSearch.Query)

    Returns:

    * puppy.model.OpenSearch.Response

    Raises:

    * urllib2.URLError

    """
  
    def create_echo_response(query):
        """Create a OpenSearch Response from the query. The response repeats the terms of the query. it is only useful for debugging
        
        Parameters:
        
        * query (str): query search terms (n.b. not a OpenSearch Query object)
        * results (dict): results from service
        
        Returns:
        
        * OpenSearch.Response
        
        """

        query_list = query.search_terms.split()

        
        response = Response()
        response.title = "Echo response"
        response.link = "www.puppyIR.eu"
        response.description = "Echo Search results"
        response.total_results = len(query_list)
        response.start_index = 1
        response.items_per_page = len(query_list)
        response.query = query
        for term in query_list:
            item = Item()
            item.title = term
            item.link = "http://www."+term+".com"
            item.description = term
            response.items.append(item)
        return response
      
       
    return create_echo_response(query)
 
